﻿using System.Collections.Generic;
using CollegeSim.CourseScheduler.Presentation.Facade;
using System;
using System.Windows.Forms;

namespace CollegeSim.CourseScheduler.Presentation
{
    // Author: Ryan Redburn
    // Project: Course Scheduler
    // Date: 4/6/2014

    public partial class FormDeleteCourse : Form
    {
        #region [ Fields ]

        private readonly SchedulerFacade _facade;
        private int _courseIndex;
        private List<string> _courseInfo; 

        #endregion

        #region [ Constructors ]

        public FormDeleteCourse(SchedulerFacade facade)
        {
            InitializeComponent();
            _facade = facade;
        }

        #endregion

        #region [ Events ]

        // Display information for the course to be deleted
        private void FormDeleteCourse_Load(object sender, EventArgs e)
        {
            // Open course select form
            using (var frmCourseSelect = new FormCourseSelect(_facade))
            {
                frmCourseSelect.ShowDialog();

                // If the user selected a course, retrieve the index value
                if (frmCourseSelect.DialogResult == DialogResult.OK)
                {
                    _courseIndex = frmCourseSelect.SelectionIndex;
                    _courseInfo = _facade.GetCourseFromTempStore(_courseIndex);
                }
                else
                {
                    Close();
                }
            }

            // Display course information
            lblCRN.Text += " " + _courseInfo[2];
            lblSubNum.Text += " " + _courseInfo[2] + _courseInfo[3];
            lblCourseTitle.Text += " " + _courseInfo[4];
            lblCredits.Text += " " + _courseInfo[5];
            lblStyle.Text += " " + _courseInfo[7];
            lblInstructor.Text += " " + _courseInfo[8];
        }

        // Confirms delete
        private void btnYes_Click(object sender, EventArgs e)
        {
            // Confirm delete
            if (MessageBox.Show("Are you sure you want to delete this course?", "Delete Course",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) return;

            // Process and display success
            var courseInfo = _facade.GetCourseFromTempStore(_courseIndex);
            _facade.DeleteCourse(int.Parse(courseInfo[0]));
            MessageBox.Show("The selected course was successfully deleted.", "Course Deleted");
            Close();
        }

        // Cancels delete
        private void btnNo_Click(object sender, EventArgs e)
        {
            Close();
        }

        #endregion
    }
}
